#ifndef __DALELIB_POVWRITER_H__
#define __DALELIB_POVWRITER_H__

#include <dalelib/graphics.h>
#include <fstream>
#include <vector>
using namespace std;

namespace dalelib
{

class POVWriter
{
private:

	ofstream _file;

public:

	POVWriter()
	{ }

	~POVWriter() { if( _file.is_open() ) close(); }

	// ********************************* accessor functions ****************************************
	ofstream& getFile() { return _file; } // should probably do some operater overloading here




	// ********************************* file functions ****************************************
	void open(const string _FileName)
	{
		ofstream myfile;
		_file.open(_FileName.c_str());
//		_file << "<?xml version=\"1.0\" standalone=\"no\"?>" << endl;
//		_file << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">" << endl;
//		_file << "<svg width=\"100%\" height=\"100%\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">" << endl;
	}

	void close()
	{
//		_file << "</svg>" << endl;
		_file.close();
	}

	void includeFile(const string _FileName)
	{
		_file << "#include \"" << _FileName << "\""<< endl;
	}

	void setBackground(const v3 _Colour=v3::one)
	{
		_file << "background { color rgb <" << _Colour[0] << "," << _Colour[1] << "," << _Colour[2] << "> }" << endl;
	}

	void setCamera(const v3 _Pos,const v3 _Tgt=v3::zero,const float _FOV=75.f,const v3 _Up=v3(0,1,0),const string _Projection="perspective")
	{
		_file << "camera { ";
		_file << _Projection << " ";
		_file << "angle " << _FOV << " ";
		_file << "location <" << _Pos[0] << "," << _Pos[1] << "," << _Pos[2] << "> ";
		_file << "look_at <" << _Tgt[0] << "," << _Tgt[1] << "," << _Tgt[2] << "> ";
		_file << "sky <" << _Up[0] << "," << _Up[1] << "," << _Up[2] << "> ";
		_file << "}" << endl;
	}

	void setFog(const v3 _Colour=v3::one,const float _Distance=500.f,const float _Offset=10.f)
	{
		_file << "fog { ";
		_file << "distance " << _Distance << " ";
		_file << "color rgb <" << _Colour[0] << "," << _Colour[1] << "," << _Colour[2] << "> ";
		_file << "fog_offset " << _Offset << " ";
		_file << "fog_alt 5 ";
		_file << "fog_type 2 ";
		_file << "}" << endl;

	}

	void addCheckeredGround()
	{
		_file << "plane { y, 0 pigment {checker color rgb 1.0, color rgb 0.9 scale 2} }" << endl;
	}

	void addLight(const v3 _Pos,const v3 _Colour=v3::one)
	{
		_file << "light_source { <" << _Pos[0] << "," << _Pos[1] << "," << _Pos[2] << "> ";
		_file << "  color rgb <" << _Colour[0] << "," << _Colour[1] << "," << _Colour[2] << "> ";
		_file << "}" << endl;
	}

	void addSphere(const float _Radius,const v3 _Pos,const v3 _Colour=v3::one)
	{
		_file << "sphere { ";
		_file <<   "<" << _Pos[0] << "," << _Pos[1] << "," << _Pos[2] << ">, ";
		_file <<   _Radius << " ";
		_file <<   "texture { ";
		_file <<     "pigment { ";
		_file <<       "color rgb <" << _Colour[0] << "," << _Colour[1] << "," << _Colour[2] << "> ";
		_file <<     "} ";
		_file <<   "} ";
		_file << "}" << endl;
	}

	void addCylinder(const float _Radius,const v3 _Start,const v3 _End,const v3 _Colour=v3::one)
	{
		_file << "cylinder { ";
		_file <<   "<" << _Start[0] << "," << _Start[1] << "," << _Start[2] << ">, ";
		_file <<   "<" << _End[0] << "," << _End[1] << "," << _End[2] << ">, ";
		_file <<   _Radius << " ";
		_file <<   "texture { ";
		_file <<     "pigment { ";
		_file <<       "color rgb <" << _Colour[0] << "," << _Colour[1] << "," << _Colour[2] << "> ";
		_file <<     "} ";
		_file <<   "} ";
		_file << "}" << endl;
	}

	void addCapsule(const float _Radius,const v3 _Start,const v3 _End,const v3 _Colour=v3::one)
	{
		addCylinder(_Radius,_Start,_End,_Colour);
		addSphere(_Radius,_Start,_Colour);
		addSphere(_Radius,_End,_Colour);
	}


};

} // namespace dalelib

#endif // __DALELIB_SVGWRITER_H__
